<template>
    <div >
    </div>
</template>
<script>
import {Base64} from 'js-base64'
import {tokenValidate} from "@/projects/basic/api/gwmfc-sso"
import util from '@/libs/util';
import {setStore} from "@/libs/utils/store";
import Constants from "@/const/common/Constants";
export default {
    name:'gwtmfc-sso',
    data(){
        return{
            component:null,
            componentPath:'',
            isFull:false,
            params:null,
            init:false,
            refreshTime:-1,
            refreshLock:false
        }
    },
    computed:{
    },
    methods:{
        errorProcess(){
            let self = this;
            this.$Spin.hide();
            this.$Message.error({
                content:'token校验失败，即将跳转到登录页',
                duration:3,
                onClose:function (){
                    if(self.$route.query.pageData){
                        let pageDate = JSON.parse(Base64.decode(self.$route.query.pageData,true));
                        let params  = {};
                        for(let key in self.$route.query){
                            if(key!=="token"&&key!=="pageData"){
                                params[key] = self.$route.query[key];
                            }
                        }
                        let data = {
                            type:Constants.auto_open_type_dlink,
                            token: {},
                            pageData: {
                                params: params,
                                component: pageDate.component,
                                isFull: pageDate.isFull,
                                pageTitle: pageDate.pageTitle,
                                eventHashKey:md5(self.$route.name)
                            }
                        };
                        setStore({name:Constants.afs_login_target_key,type:Constants.session_store_type,content:data});
                    }
                    self.$router.replace("/");
                }
            })
        },
        initToken(token){
            tokenValidate(token).then(res=>{
                if(res.code==='0000'){
                    if(res.data.validate&&res.data.token){
                        this.$store.commit('SET_ACCESS_TOKEN',res.data.token.access_token)
                        this.$store.commit('SET_REFRESH_TOKEN',res.data.token.refresh_token)
                        this.$store.commit('SET_EXPIRES_IN',res.data.token.expires_in)
                        this.$store.commit('SET_SSO_ACCESS_TOKEN',res.data.ssoToken)
                        this.$store.commit('SET_SSO_EXPIRES_IN',res.data.tokenExpireTime)
                        this.$store.dispatch('GetUserInfo').then(()=>{
                            this.$Spin.hide();
                            //获取用户信息后进行页面跳转或者组件加载判断
                            if(!this.$route.query.pageData){
                                console.log("无，pageData参数，进入home页");
                                util.initSystem(this);
                                this.$router.replace("/");
                                return
                            }
                            // 其他情况加载页面数据,跳转到BlankMain组件，
                            let pageDate = JSON.parse(Base64.decode(this.$route.query.pageData,true));
                            let params  = {};
                            for(let key in this.$route.query){
                                if(key!=="token"&&key!=="pageDate"){
                                    params[key] = this.$route.query[key];
                                }
                            }
                            const token = {
                                accessToken:res.data.token.access_token,
                                refreshToken:res.data.token.refresh_token,
                                expiresIn:res.data.token.expires_in * 1000 + (Date.now())
                            };
                            let data = {
                                token: token,
                                pageData: {
                                    params: params,
                                    component: pageDate.component,
                                    isFull: pageDate.isFull,
                                    pageTitle: pageDate.pageTitle,
                                    eventHashKey:md5(this.$route.name)
                                }
                            };
                            this.$router.replace('/d/afsLink?_link=' + Base64.encode(JSON.stringify(data), true));
                        })
                    }else {
                        this.errorProcess();
                    }
                }else{
                    this.errorProcess();
                }
            }).catch(()=>{
                this.errorProcess();
            })
        }
    },
    created(){
        // 参数 token--统一登录平台token，pageData-页面信息JSON 串base64 编码后结果 json 包含，component:组件路径，isFull:是否全路径;tilte-页面title,其他参数--业务组件自己约定，符合uri 链接 a=b&c=d格式
        this.$Spin.show();
        const token = this.$route.query.token;
        //token为空直接进入根页面
        if(!token||token===''){
            this.errorProcess();
        }else{
            this.initToken(token);
        }
    },
    mounted(){

    }
}
</script>
